package LanQiao._15JavaB;

import java.util.*;

public class _8 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer[]> r0 = new ArrayList<Integer[]>();
        List<Integer[]> y1 = new ArrayList<Integer[]>();
        List<Integer[]> b2 = new ArrayList<Integer[]>();
        for (int i = 0; i < n; i++) {
            int li = sc.nextInt();
            int wi = sc.nextInt();
            int color = sc.nextInt();
            if (color == 0) {
                r0.add(new Integer[]{li, wi});
            }else if (color == 1) {
                y1.add(new Integer[]{li, wi});
            }else {
                b2.add(new Integer[]{li, wi});
            }
        }
        r0.sort(new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return o1[0] - o2[0] == 0 ?  o1[1] - o2[1] : o1[0] - o2[0];
            }
        });
        y1.sort(new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return o1[0] - o2[0] == 0 ?  o1[1] - o2[1] : o1[0] - o2[0];
            }
        });
        b2.sort(new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return o1[0] - o2[0] == 0 ?  o1[1] - o2[1] : o1[0] - o2[0];
            }
        });
        long res = 0;
        int mod = 1000000007;
        int size0 = r0.size();
        int size1 = y1.size();
        int size2 = b2.size();
        if (size0 <= size1 && size0 <= size2) {
            for (Integer[] integers : r0) {
                Integer li = integers[0];
                Integer wi = integers[1];
                int i1 = helper1(li, wi, y1);
                res += i1 % mod;
                res %= mod;
                int i2 = helper1(li, wi, b2);
                res += i2 % mod;
                res %= mod;
                int j1 = helper2(li, wi, y1);
                res += j1 % mod;
                res %= mod;
                int j2 = helper2(li, wi, b2);
                res += j2 % mod;
                res %= mod;
            }
            if (size1 < size2){
                for (Integer[] integers : y1) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, b2);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, b2);
                    res += j1 % mod;
                    res %= mod;
                }
            }else {
                for (Integer[] integers : b2) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, y1);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, y1);
                    res += j1 % mod;
                    res %= mod;
                }
            }
        }else if (size1 <= size2) {
            for (Integer[] integers : y1) {
                Integer li = integers[0];
                Integer wi = integers[1];
                int i1 = helper1(li, wi, r0);
                res += i1 % mod;
                res %= mod;
                int i2 = helper1(li, wi, b2);
                res += i2 % mod;
                res %= mod;
                int j1 = helper2(li, wi, r0);
                res += j1 % mod;
                res %= mod;
                int j2 = helper2(li, wi, b2);
                res += j2 % mod;
                res %= mod;
            }
            if (size0 < size2){
                for (Integer[] integers : r0) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, b2);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, b2);
                    res += j1 % mod;
                    res %= mod;
                }
            }else {
                for (Integer[] integers : b2) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, r0);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, r0);
                    res += j1 % mod;
                    res %= mod;
                }
            }
        }else {
            for (Integer[] integers : b2) {
                Integer li = integers[0];
                Integer wi = integers[1];
                int i1 = helper1(li, wi, r0);
                res += i1 % mod;
                res %= mod;
                int i2 = helper1(li, wi, y1);
                res += i2 % mod;
                res %= mod;
                int j1 = helper2(li, wi, r0);
                res += j1 % mod;
                res %= mod;
                int j2 = helper2(li, wi, y1);
                res += j2 % mod;
                res %= mod;
            }
            if (size0 < size1){
                for (Integer[] integers : r0) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, y1);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, y1);
                    res += j1 % mod;
                    res %= mod;
                }
            }else {
                for (Integer[] integers : y1) {
                    Integer li = integers[0];
                    Integer wi = integers[1];
                    int i1 = helper1(li, wi, r0);
                    res += i1 % mod;
                    res %= mod;
                    int j1 = helper2(li, wi, r0);
                    res += j1 % mod;
                    res %= mod;
                }
            }
        }
        System.out.println(res);
        sc.close();
    }

    //比li大 wi小
    public static int helper1(int taget1,int taget2,List<Integer[]> list){
        int count1 = 0,count2 = 0;
        int left = 0, right = list.size() - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (list.get(mid)[0] > taget1) {
                right = mid;
            }else {
                left = mid + 1;
            }
        }
        if (left == right && list.get(right)[0] <= taget1){
            right++;
        }
        count1 = list.size() - right;
        if (count1 == 0) return 0;
        int[] wi = new int[count1];
        int t = right;
        for (; right < list.size(); right++) {
            wi[right - t] = list.get(right)[1];
        }
        Arrays.sort(wi);
        left = 0;
        right = count1 - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (wi[mid] < taget2) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        count2 = right + 1;
        return count2;
    }
    //比li小 wi大
    public static int helper2(int taget1,int taget2,List<Integer[]> list){
        int count1 = 0,count2 = 0;
        int left = 0, right = list.size() - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (list.get(mid)[0] < taget1) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        count1 = right + 1;
        if (count1 == 0) return 0;
        int[] wi = new int[count1];
        for (int i = 0; i < count1; i++) {
            wi[i] = list.get(i)[1];
        }
        Arrays.sort(wi);
        left = 0;
        right = count1 - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (wi[mid] > taget2) {
                right = mid;
            }else {
                left = mid + 1;
            }
        }
        if (left == right && wi[right] <= taget2){
            right++;
        }
        count2 = list.size() - right;
        return count2;
    }

    //他解： 树状数组
    public class Solution {
        static class FenwickTree {
            int n;
            int[] tree;

            public FenwickTree(int n) {
                this.n = n;
                tree = new int[n + 1];
            }

            int lowbit(int i) {
                return i & -i;
            }

            void add(int i, int val) {
                for (; i <= n; i += lowbit(i)) {
                    tree[i] += val;
                }
            }

            int preSum(int i) {
                int ret = 0;
                for (; i > 0; i -= lowbit(i)) {
                    ret += tree[i];
                }
                return ret;
            }

            int rangeSum(int l, int r) {
                return preSum(r) - preSum(l - 1);
            }
        }

        static int maxn = 100010;
        static int ans = 0;
        static int mod = (int) 1e9 + 7;

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            List<int[]> arr = new ArrayList<>();

            // 读入所有的矩形
            for (int i = 0; i < n; i++) {
                int l = sc.nextInt(); // 长
                int w = sc.nextInt(); // 宽
                int c = sc.nextInt(); // 颜色
                arr.add(new int[]{l, w, c});
            }

            // 对矩形进行排序，l升序，然后w升序
            arr.sort((o1, o2) -> {
                if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
                else return Integer.compare(o1[1], o2[1]);
            });

            // 建立三种颜色对应的树状数组
            FenwickTree[] tree = new FenwickTree[3];
            for (int i = 0; i < 3; i++) tree[i] = new FenwickTree(maxn);

            // 然后枚举排序好的arr
            for (int[] a : arr) {
                int l = a[0];
                int w = a[1];
                int c = a[2];
                for (int i = 0; i < 3; i++) {
                    if (c == i) continue; // 相同颜色被排除掉
                    ans = (ans + tree[i].rangeSum(w + 1, maxn)) % mod; // 累加答案
                }
                tree[c].add(w, 1); // 更新树状数组
            }
            System.out.println(ans);
        }
    }

}
